﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;

namespace Business.DAO
{
    public class DiemDAO
    {
        private ISSContext db = new ISSContext();
        public List<Diem> GetAll()
        {
            return db.Diem.ToList();
        }

        public Diem Get(int id)
        {
            Diem diem = db.Diem.Find(id);
            return diem;
        }

        public List<Diem> GetByHocSinh(HocSinh hocsinh)
        {
            var query = from diem in db.Diem where diem.HocSinh == hocsinh select diem;
            List<Diem> lstDiem = query.ToList<Diem>();
            return lstDiem;
        }

        public List<Diem> GetByMonHoc_LopHoc(MonHoc_LopHoc MonHoc_LopHoc)
        {
            var query = from diem in db.Diem where diem.MonHoc_LopHoc == MonHoc_LopHoc select diem;
            List<Diem> lstDiem = query.ToList<Diem>();
            return lstDiem;
        }

        public List<Diem> GetByHocSinh_MonHoc_LopHoc(HocSinh hocsinh, MonHoc_LopHoc MonHoc_LopHoc)
        {
            var query = from diem in db.Diem where diem.HocSinh==hocsinh && diem.MonHoc_LopHoc == MonHoc_LopHoc select diem;
            List<Diem> lstDiem = query.ToList<Diem>();
            return lstDiem;
        }

        public int Create(Diem diem)
        {
            try
            {
                db.Diem.Add(diem);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(Diem diem)
        {
            try
            {
                db.Entry(diem).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                Diem diem = db.Diem.Find(id);
                db.Diem.Remove(diem);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}